import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.InflaterInputStream;

public class h_7 {
    public static void main(String[] args) throws Exception {
        String xingStr="https://www.jimingbao.com/90981.html";
        String manStr="http://www.haoming8.cn/mingzi/59965.html";
        String womanStr="http://www.haoming8.cn/mingzi/59967.html";

        String webCrawlerXing = getWeb(xingStr);
        String manMing = getWeb(manStr);
        String womanMing=getWeb(womanStr);
//		System.out.println(webCrawlerXing);

        //通过正则表达式，把其中符号要求的数据获取出来
        ArrayList<String> dataXing = getData(webCrawlerXing,"(.、){2}");
        ArrayList<String> dataManMing = getData(manMing,"(.. ){4}..");
        ArrayList<String> dataWomanMing = getData(womanMing,"(.. ){4}..");
        System.out.println(dataXing);
        System.out.println(dataManMing);
        System.out.println(dataWomanMing);
        //处理姓氏
        ArrayList<String> xingList=getList(dataXing,"、");

        System.out.println(xingList);
        //处理名字
        ArrayList<String> manMingList=getList(dataManMing," ");
        ArrayList<String> womanMingList=getList(dataWomanMing," ");

        System.out.println(manMingList);
        System.out.println(womanMingList);

        //自由生成名字
        ArrayList<String> maninfos = getInfos(xingList,manMingList,"男",50,20,25);
        ArrayList<String> womaninfos = getInfos(xingList,womanMingList,"女",50,18,22);
        ArrayList<String> list=new ArrayList<>();
        list.addAll(maninfos);list.addAll(womaninfos);
        System.out.println(list);
        Collections.shuffle(list);

        //写出数据
        BufferedWriter bw=new BufferedWriter(new FileWriter("names.txt"));
        for (String string : list) {
            bw.write(string);
            bw.newLine();
        }
        bw.close();
    }

    public static ArrayList<String> getList(ArrayList<String> list,String regex ){
        ArrayList<String> getlist=new ArrayList();
        for (String s : list) {
            //李、王、
            String[] split = s.split(regex);
            for(int i=0;i<split.length;i++){
                getlist.add(split[i]);
            }
        }
        return getlist;
    }
    private static ArrayList<String> getInfos(ArrayList<String> xingList, ArrayList<String> mingList,String sex, int count,int ageStart,int ageEnd) {
        HashSet<String> set=new HashSet<>();
        while(true){
            if(set.size()==count){
                break;
            }
            //随机
            Collections.shuffle(mingList);
            Collections.shuffle(xingList);
            Random random=new Random();
            int age=random.nextInt(ageEnd-ageStart)+ageStart;
            set.add(xingList.get(0)+mingList.get(0)+"-"+sex+"-"+age);
        }
        ArrayList<String> list=new ArrayList<>(set);
        return list;
    }
    public static ArrayList<String> getData(String str,String regex){
        ArrayList<String> list=new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while(matcher.find()){
            String group = matcher.group();
            list.add(group);
        }
        return list;
    }
    public static String getWeb(String str) throws Exception {
        StringBuffer sb=new StringBuffer();
        URL url=new URL(str);
        URLConnection conn = url.openConnection();
        InputStreamReader in=new InputStreamReader(conn.getInputStream(),"UTF-8");
        int ch=0;
        while((ch=in.read())!=-1){
            sb.append((char)ch);
        }
        return sb.toString();
    }
}
